package cn.yuanqiao.archive.storeRoom.service.impl;

import cn.yuanqiao.archive.storeRoom.domain.Grid;
import cn.yuanqiao.archive.storeRoom.domain.Shelf;
import cn.yuanqiao.archive.storeRoom.mapper.GridMapper;
import cn.yuanqiao.archive.storeRoom.mapper.ShelfMapper;
import cn.yuanqiao.archive.storeRoom.service.IShelfService;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

;

/**
 * 档案柜Service业务层处理
 * 
 * @author xy
 * @date 2023-06-19
 */
@Service
public class ShelfServiceImpl implements IShelfService
{
    @Autowired
    private ShelfMapper shelfMapper;

    @Autowired
    private GridMapper gridMapper;

    /**
     * 查询档案柜
     * 
     * @param shelfId 档案柜主键
     * @return 档案柜
     */
    @Override
    public Shelf selectShelfByShelfId(Long shelfId)
    {
        Grid grid=new Grid();
        grid.setShelfId(shelfId);
        String tenantCode = SecurityUtils.getTenantCode();
        if(StringUtils.isEmpty(tenantCode)){
            Shelf shelf= shelfMapper.selectShelfByShelfId(shelfId,"");
            //shelf.setGridList(gridMapper.selectGridList(grid));
            return shelf;
        }else{

            Shelf shelf= shelfMapper.selectShelfByShelfId(shelfId,tenantCode.toUpperCase());
           // shelf.setGridList(gridMapper.selectGridList(grid));
            return shelf;
        }

    }

    /**
     * 查询档案柜列表
     * 
     * @param shelf 档案柜
     * @return 档案柜
     */
    @Override
    public List<Shelf> selectShelfList(Shelf shelf)
    {
        String tenantCode = SecurityUtils.getTenantCode();
        if(StringUtils.isEmpty(tenantCode)) {
            return shelfMapper.selectShelfList(shelf,"");
        }else{
            return shelfMapper.selectShelfList(shelf,tenantCode.toUpperCase());

        }
    }

    /**
     * 新增档案柜
     * 
     * @param shelf 档案柜
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertShelf(Shelf shelf)
    {
        //验证编码
        String shelfCode=shelf.getShelfCode();
        int count=shelfMapper.getCountByShelfCode(shelfCode,SecurityUtils.getTenantCode().toUpperCase(),shelf.getStoreId());
        if(count>0){
          throw new ServiceException("档案柜编码已存在!");
        }
        Long shelfId=SnowflakeIdGenerator.generateId();
        shelf.setCreateTime(DateUtils.getNowDate());
        String tenantCode = SecurityUtils.getTenantCode();
        shelf.setShelfId(shelfId);
        shelf.setIsDel(0);
        int n=0;
        if(StringUtils.isEmpty(tenantCode)) {
            n=shelfMapper.insertShelf(shelf,"");
        }else{
            n=shelfMapper.insertShelf(shelf,tenantCode.toUpperCase());
        }
        int cross=shelf.getShelfCross();
        int stand=shelf.getShelfStand();
        int side=1;
         String gridSide="";
        if(shelf.getShelfSide().length()>1) {
            side=2;
        }
        int num=0;
        int gridWidth=(shelf.getShelfWidth()*10)/cross;//格子宽度
        int syWidth=(shelf.getShelfWidth()*10)/cross;//剩余宽度
        for(int x=1;x<=side;x++) {//AB面
            if(x==2) {
                num=0;
            }
            for(int j=1;j<=stand;j++) {//列
                for(int i=1;i<=cross;i++) {//行
                    Long gridId=SnowflakeIdGenerator.generateId();
                    int grid_crsnum=i;
                    int grid_stdnum=j;
                    String gridCode =++num+"格";
                    String gridName="";//列grid_name
                    if(shelf.getShelfSide().equals("A")){
                        gridName="A面 "+i+"排 "+j+"列";
                        gridSide="A";
                    }else if(shelf.getShelfSide().equals("B")){
                        gridName="B面 "+i+"排 "+j+"列";
                        gridSide="B";
                    }else if(x==1){
                        gridName="A面 "+i+"排 "+j+"列";
                        gridSide="A";
                    }else {
                        gridName="B面 "+i+"排 "+j+"列";
                        gridSide="B";
                    }
                    Grid grid=new Grid();
                    grid.setCreator(SecurityUtils.getUserId());
                    grid.setCreateTime(DateUtils.getNowDate());
                    grid.setGridId(gridId);
                    grid.setGridCode(gridCode);
                    grid.setGridName(gridName);
                    grid.setGridSide(gridSide);
                    grid.setShelfId(shelfId);
                    grid.setGridWidth(gridWidth);
                    grid.setGridPrewidth(0);
                    grid.setGridCrsnum(grid_crsnum);
                    grid.setGridStdnum(grid_stdnum);
                    grid.setSyWidth(syWidth);
                    grid.setUseWidth(0);
                    grid.setPutawayNum(0);
                    n+=gridMapper.insertGrid(grid);

                }
            }
        }

         return n;

    }

    /**
     * 修改档案柜
     * 
     * @param shelf 档案柜
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateShelf(Shelf shelf)
    {
        String tenantCode = SecurityUtils.getTenantCode();
        shelf.setLastupdater(SecurityUtils.getUserId());
        shelf.setLastupdateTime(DateUtils.getNowDate());
        int n=0;
        if(StringUtils.isEmpty(tenantCode)) {
             n=shelfMapper.updateShelf(shelf,"");
        }else{
             n=shelfMapper.updateShelf(shelf,tenantCode.toUpperCase());
        }
        // 修改柜子
        int gridCount=gridMapper.getGridCount(shelf.getShelfId(),tenantCode.toUpperCase());
        if(gridCount<=0){
            //进行修改格子
            gridMapper.deleteGridByShelfId(shelf.getShelfId(),tenantCode.toUpperCase());
            int cross=shelf.getShelfCross();
            int stand=shelf.getShelfStand();
            int side=1;
            String gridSide="";
            if(shelf.getShelfSide().length()>1) {
                side=2;
            }
            int num=0;
            int gridWidth=(shelf.getShelfWidth()*10)/cross;//格子宽度
            int syWidth=(shelf.getShelfWidth()*10)/cross;//剩余宽度
            for(int x=1;x<=side;x++) {//AB面
                if(x==2) {
                    num=0;
                }
                for(int j=1;j<=stand;j++) {//列
                    for(int i=1;i<=cross;i++) {//行
                        Long gridId=SnowflakeIdGenerator.generateId();
                        int grid_crsnum=i;
                        int grid_stdnum=j;
                        String gridCode =++num+"格";
                        String gridName="";//列grid_name
                        if(shelf.getShelfSide().equals("A")){
                            gridName="A面 "+i+"排 "+j+"列";
                            gridSide="A";
                        }else if(shelf.getShelfSide().equals("B")){
                            gridName="B面 "+i+"排 "+j+"列";
                            gridSide="B";
                        }else if(x==1){
                            gridName="A面 "+i+"排 "+j+"列";
                            gridSide="A";
                        }else {
                            gridName="B面 "+i+"排 "+j+"列";
                            gridSide="B";
                        }
                        Grid grid=new Grid();
                        grid.setLastupdater(SecurityUtils.getUserId());
                        grid.setLastupdateTime(DateUtils.getNowDate());
                        grid.setGridId(gridId);
                        grid.setGridCode(gridCode);
                        grid.setGridName(gridName);
                        grid.setGridSide(gridSide);
                        grid.setShelfId(shelf.getShelfId());
                        grid.setGridWidth(gridWidth);
                        grid.setGridPrewidth(0);
                        grid.setGridCrsnum(grid_crsnum);
                        grid.setGridStdnum(grid_stdnum);
                        grid.setSyWidth(syWidth);
                        grid.setUseWidth(0);
                        grid.setPutawayNum(0);
                        n+=gridMapper.insertGrid(grid);

                    }
                }
            }


        }


        return n;
    }

    /**
     * 批量删除档案柜
     * 
     * @param shelfIds 需要删除的档案柜主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteShelfByShelfIds(Long[] shelfIds)
    {
        String tenantCode = SecurityUtils.getTenantCode();
        if(StringUtils.isEmpty(tenantCode)) {
            gridMapper.deleteGridByShelfIds(shelfIds,"");
            return shelfMapper.deleteShelfByShelfIds(shelfIds,"");
        }else{
            gridMapper.deleteGridByShelfIds(shelfIds,tenantCode.toUpperCase());
            return shelfMapper.deleteShelfByShelfIds(shelfIds,tenantCode.toUpperCase());
        }
    }

    /**
     * 删除档案柜信息
     * 
     * @param shelfId 档案柜主键
     * @return 结果
     */
    @Override
    public int deleteShelfByShelfId(Long shelfId)
    {
        String tenantCode = SecurityUtils.getTenantCode();
        if(StringUtils.isEmpty(tenantCode)) {
           return shelfMapper.deleteShelfByShelfId(shelfId,"");
        }else{
            return shelfMapper.deleteShelfByShelfId(shelfId,tenantCode.toUpperCase());
        }
    }

    @Override
    public int checkShelf(Long shelfId) {
        String tenantCode = SecurityUtils.getTenantCode();
        if(StringUtils.isEmpty(tenantCode)) {
            return gridMapper.getGridCount(shelfId,"");
        }else{
            return gridMapper.getGridCount(shelfId,tenantCode.toUpperCase());

        }
    }

    @Override
    public List<Grid> getGridInfo(Long shelfId) {
        Grid grid=new Grid();
        grid.setShelfId(shelfId);
        return gridMapper.selectGridList(grid,SecurityUtils.getTenantCode().toUpperCase());
    }
}
